package com.hm.health.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import androidx.recyclerview.widget.RecyclerView;

import com.hm.health.R;
import com.hm.health.entity.BleDevicesInfo;

import java.util.List;

public class ToggleButtonAdapter extends RecyclerView.Adapter<ToggleButtonAdapter.ViewHolder> {

    private Context mContext;

    private List<BleDevicesInfo> mlistData;

    public ToggleButtonAdapter(Context context,
                               List<BleDevicesInfo> listDamondsItem) {
        mContext = context;
        mlistData = listDamondsItem;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.toggle_button, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (null == mlistData
                || mlistData.size() < position) {
            return;
        }

        // 描述信息
        String strDeviceName;
        if ((mlistData.get(position).getDeviceName() == null)
                || (mlistData.get(position).getDeviceName().isEmpty())) {
            strDeviceName = mContext.getString(R.string.unknown_device);
        } else {
            strDeviceName = mlistData.get(position).getDeviceName();
        }

        holder.mRadioButton.setText(strDeviceName);

        // is connected
        if (mlistData.get(position).isConnected()) {
            holder.mRadioButton.setChecked(true);
           holder.mRadioButton.setTextColor(mContext.getResources().getColor(R.color.device_connected));
        } else {
            holder.mRadioButton.setChecked(false);
            holder.mRadioButton.setTextColor(mContext.getResources().getColor(R.color.black));
        }
    }

    @Override
    public int getItemCount() {
        if (null != mlistData) {
            return mlistData.size();
        } else {
            return 0;
        }
    }

    /**
     * 界面视图
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public RadioButton mRadioButton;

        public ViewHolder(View itemView) {
            super(itemView);
            mRadioButton = itemView.findViewById(R.id.rbDevice);
        }
    }
}
